package cn.ligoo.part.web.springmvc.platform;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ligoo.part.domain.TypeInfo;
import cn.ligoo.part.service.impl.FacadeImpl;
import cn.ligoo.part.web.Constants;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/platform/type-info")
public class TypeInfoController {

	private final Logger logger = LoggerFactory.getLogger(TypeInfoController.class);

	@Resource
	private MessageSource message;

	@Resource
	private FacadeImpl facade;

	public TypeInfoController() {
		logger.debug("...constructor");
	}

	@RequestMapping(value = "/search", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String search(HttpServletRequest request, @RequestParam int page, @RequestParam int rows, TypeInfo t) {
		logger.debug("...search");

		logger.debug("...page:{}, rows:{}", page, rows);
		logger.debug("...typeInfo:{}", t.toString());

		Long total = facade.getTypeInfoService().getTypeInfoCount(t);

		t.get_rowBounds().setOffset((page - 1) * rows);
		t.get_rowBounds().setLimit(rows);
		List<TypeInfo> typeInfoList = facade.getTypeInfoService().getTypeInfoList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(typeInfoList), total);

		logger.debug("...json:{}", json);
		return json;
	}

	@RequestMapping(value = "/json", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String json(@RequestParam String type_sign) {
		logger.debug("...json");

		TypeInfo t = new TypeInfo();
		t.setType_sign(type_sign);
		t.setIs_del(Constants.BYTE_0);
		logger.debug("...typeInfo:{}", t.toString());

		List<TypeInfo> typeInfoList = facade.getTypeInfoService().getTypeInfoList(t);

		String json = JSON.toJSONString(typeInfoList);
		logger.debug("...json:{}", json);
		return json;
	}

	@RequestMapping(value = "/format", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	public String format(Model model) {
		logger.debug("...json");

		TypeInfo t = new TypeInfo();
		t.setIs_del(Constants.BYTE_0);
		logger.debug("...typeInfo:{}", t.toString());

		List<TypeInfo> typeInfoList = facade.getTypeInfoService().getTypeInfoGroupList(t);
		logger.info("typeInfoList.size():={}", typeInfoList.size());
		for (TypeInfo ti : typeInfoList) {
			logger.info("{}|{}|{}", new Object[] { ti.getType_sign(), ti.getType_value(), ti.getType_name() });
			ti.setType_sign(StringUtils.capitalize(ti.getType_sign().toLowerCase()));
		}
		model.addAttribute("typeInfoList", typeInfoList);
		return "/platform/__format";
	}

	@RequestMapping(value = "/delete", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String delete(@RequestParam String[] ids) {
		logger.debug("...delete ids:{}", ids);
		TypeInfo t = new TypeInfo();
		t.get_map().put("_ids", ids);
		t.get_map().put("_isLogicalDelete", "true");

		int count = 0;
		try {
			count = facade.getTypeInfoService().removeTypeInfo(t);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, count);
	}

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String save(TypeInfo typeInfo) {
		logger.debug("...save");
		logger.debug("typeInfo:{}", typeInfo.toString());
		try {
			if (null == typeInfo.getId()) {
				facade.getTypeInfoService().createTypeInfo(typeInfo);
			} else {
				facade.getTypeInfoService().modifyTypeInfo(typeInfo);
			}
		} catch (DuplicateKeyException dke) {
			String msg = message.getMessage("errors.dae.DuplicateKeyException", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, null);
	}

	@RequestMapping(value = "/edit/{id}", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String edit(@PathVariable Long id) {
		logger.debug("...edit");
		TypeInfo t = new TypeInfo();
		t.setId(id);
		TypeInfo typeInfo = facade.getTypeInfoService().getTypeInfo(t);
		logger.debug("...typeInfo:{}", typeInfo);
		return JSON.toJSONString(typeInfo);
	}
}
